﻿
window.messages = {
    "gameStart": { "type": 0, "name": "game started" },
    "repairAction": { "type": 1, "name": "repair action" },
    "maintenaneAction": { "type": 2, "name": "maintenane action" },
    "maintenaneTime": { "type": 3, "name": "maintenane time" },
    "feulFillDone": { "type": 4, "name": "fuel fill" },
    "feulFillBySpaceShipDone": { "type": 5, "name": "fuel fill by spaceship" },
    "feulStationCreated": { "type": 6, "name": "fuel station created" },
    "mechanicalConditionChanged": { "type": 7, "name": "mechanical condition changed" },
    "timeToMaintenance": { "type": 9, "name": "time to do maintenance" },
    "meteorSummery": { "type": 10, "name": "meteorSummery" },
    "alertBeforeDisplayed": { "type": 11, "name": "alertBeforeDisplayed" },
    "gameEnd": { "type": 99, "name": "game end" },
};

var enumAction = {

    "MAINTENANCE": "MAINTENANCE",
    "REPAIR": "REPAIR",
    "REFULING": "REFULING",
    "WAIT_TO_FUEL_SHIP": "WAIT_TO_FUEL_SHIP",
    "REFULING_BY_FUEL_SHIP": "REFULING_BY_FUEL_SHIP",

    "MALFUNCTION_OCCUR": "MALFUNCTION_OCCUR",
    "SHUTDOWN": "SHUTDOWN",
    "COLISSION": "COLISSION",
};


var enumMC = {
    "FIX": 0,
    "MINOR": 1,
    "MODERATE": 2,
    "SEVERE": 3
};

function CostTable() {

    this.repairCost = [0, 500, 250, 500];
    this.repairTime = [0, 3 * 1000, 8 * 1000, 15 * 1000]; //milliseconds

    this.maintenanceCost = 5;
    this.maintenanceTime = 8 * 1000; //milliseconds

    this.refuelingCost = 0;
    this.refuelingTime = 0;

    this.refuelingBySpaceshipCost = 300;
    this.refuelingBySpaceshipTime = 3 * 1000;
    this.waitToFuelShip = 7 * 1000;

    this.getTime = function (action) {
        switch (action) {
            case enumAction.MAINTENANCE:
                return this.maintenanceTime;
            case enumAction.REFULING:
                return this.refuelingTime;
            case enumAction.REFULING_BY_FUEL_SHIP:
                return this.refuelingBySpaceshipTime;
            case enumAction.WAIT_TO_FUEL_SHIP:
                return this.waitToFuelShip;
        }
    };

    this.getCost = function (action) {
        switch (action) {
            case enumAction.MAINTENANCE:
                return this.maintenanceCost;
                break;
            case enumAction.REFULING:
                return this.refuelingCost;
                break;
            case enumAction.REFULING_BY_FUEL_SHIP:
                return this.refuelingBySpaceshipCost;
                break;
        }
    };

    this.getRefuelingCost = function (maxFuelInventory, fuelState) {
        var missingFuel = (maxFuelInventory - fuelState);
        return parseInt(missingFuel / 1000);
    }

    this.getRepairTime = function (mc) {
        return this.repairTime[mc];
    };

    this.getRepairCost = function (mc) {
        return this.repairCost[mc];
    };
};

function TimeSampler() {

    this.nextTime = 0;
    this.sampleValue = 0;

    this.getNextTime = function () {
        return this.nextTime;
    };

    this.sampling = function (t0) {
        this.sampleValue = this.innerSampling();
        this.nextTime = t0 + this.sampleValue;
        return this.sampleValue;
    };

    this.setNextTime = function (time) {
        //This method should be called just when you want to force the next time (used in tutorial - controllerPreview.js).
        this.nextTime = time;
    };

    this.shift = function (time) {
        this.nextTime += time;
        return this.nextTime;
    };

    // should to implemented by inner objects.
    this.innerSampling = function () {
        return 999999;
    };

}

ConstTimeSampler = function (interval) {
    this.interval = interval;

    this.innerSampling = function () {
        return this.interval;
    };

};
ConstTimeSampler.prototype = new TimeSampler();

BetaTimeSampler = function (range) {

    this.range = range;

    this.innerSampling = function () {
        return parseInt(Math.sqrt(Math.random()) * this.range);
    };

};
BetaTimeSampler.prototype = new TimeSampler();

function Scenario() {

    this.gameLength = 6 * 60 * 1000;
    this.maxFeulInventory = 90 * 1000;

    this.spaceshipSpeed = [12, 12, 7, 4];
    this.fireRate = [15, 15, 25, 50]; //FIX, MINOR, MODERATE, SEVERE

    this.hitByMeteorScore = 10;
    this.meteorScore = 30;

    this.costTable = new CostTable();
    this.mechanicalEventSampler = new BetaTimeSampler(52000);
    this.fuelStationSampler = new BetaTimeSampler(40 * 1000);

    this.maintenanceAlert = new ConstTimeSampler(20 * 1000);

    this.getShootingBreakPercent = function (currentMC) {
        return this.fireRate[currentMC];
    };

    this.setGameLength = function (gameLength) {
        this.gameLength = gameLength;
    };

    this.getCostTable = function () {
        return this.costTable;
    };

    this.getSpeed = function (currentMC) {
        return this.spaceshipSpeed[currentMC];
    };

    this.getMaxFeulInventory = function () {
        return this.maxFeulInventory;
    };

    this.shiftSamplers = function (shiftedTime) {
        this.mechanicalEventSampler.shift(shiftedTime);
        this.fuelStationSampler.shift(shiftedTime);
        this.maintenanceAlert.shift(shiftedTime);
    };

    this.init = function () {
        this.mechanicalEventSampler.sampling(0);
        this.fuelStationSampler.sampling(0);
        this.maintenanceAlert.sampling(0);
    };

}

var newScenario = new Scenario();

function getNewScenario() {
    return newScenario;
}